package com.bdqn.t382.dao;

// Main.java

import com.bdqn.t382.sz.Operation;
import com.bdqn.t382.sz.factory.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    private static final Map<Character, OperationFactory> factories = new HashMap<>();

    static {
        factories.put('+', new AddOperationFactory());
        factories.put('-', new SubtractOperationFactory());
        factories.put('*', new MultiplyOperationFactory());
        factories.put('/', new DivideOperationFactory());
        factories.put('%', new ModulusOperationFactory());
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入第一个数字: ");
        double num1 = scanner.nextDouble();

        System.out.print("请输入运算符 (+, -, *, /, %): ");
        char operator = scanner.next().charAt(0);

        System.out.print("请输入第二个数字: ");
        double num2 = scanner.nextDouble();

        OperationFactory factory = factories.get(operator);
        if (factory == null) {
            System.out.println("无效的运算符: " + operator);
            scanner.close();
            return;
        }

        Operation operation = factory.createOperation();

        double result;
        try {
            result = operation.calculate(num1, num2);
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
            scanner.close();
            return;
        }

        System.out.println("运算结果: " + result);

        scanner.close();
    }
}